//
//  CategoryViewController.m
//  IntimateGift
//
//  Created by zh on 14/12/17.
//  Copyright (c) 2014年 蔡宏业. All rights reserved.
//

#import "CategoryViewController.h"
#import "NetHandler.h"
#import "Special.h"
#import "UIImageView+WebCache.h"
#import "AllGroup.h"
#import "SpecialDetailViewController.h"
#import "ObjectViewController.h"
#import "PersoniltyViewController.h"
#import "OccassionViewController.h"
#import "AllButtonController.h"
#import "MMDrawerBarButtonItem.h"
#import "UIViewController+MMDrawerController.h"
#import "UIColor+AddColor.h"


#define Weight 35

@class Picture;
@interface CategoryViewController ()
@property(nonatomic, retain)NSMutableArray *speciallArr;
@property(nonatomic, retain)UIScrollView *allScrollView;
@property(nonatomic, retain)NSMutableDictionary *categorydic;
@property(nonatomic, retain)UIScrollView *obScrollView;
@property(nonatomic, retain)UIScrollView *personiltyScrollView;
@property(nonatomic, retain)UIScrollView *occassionScrollView;
@property(nonatomic, retain)UIScrollView *specialScroll;
@end

@implementation CategoryViewController
- (void)dealloc
{
    [_speciallArr release];
    [_allScrollView release];
    [_categorydic release];
    [_personiltyScrollView release];
    [_obScrollView release];
    [_occassionScrollView release];
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.categorydic = [NSMutableDictionary dictionary];
        
    }
        return self;
}




- (void)handleData
{
    
    //专题数据的网络请求
    //专题图片解析

    
    //专题数据的网络请求

    //专题图片解析

    NSString *str = @"http://api.liwushuo.com/v1/collections?limit=12&offset=0";
    [NetHandler getDataWithUrl:str completion:^(NSData *data) {
        NSError *error = nil;
        id  result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        
        NSDictionary *dic = [[NSDictionary alloc]init];
        dic = [result objectForKey:@"data"];
        NSArray *arr = [NSArray array];
        arr = [dic objectForKey:@"collections"];
        self.speciallArr = [NSMutableArray array];
        //        Special *hehe = [[Special alloc]init];
        for (NSDictionary *dictemp in arr) {
            //利用字典将对象初始化
            Special *hehe =[[Special alloc]initWithDictionary:dictemp];
            [self.speciallArr addObject:hehe];
            
        }
        [self handleDataone];
        [self specialScrollView];
        }];
    //对象，个性，场合的网络数据请求
    
    
}
- (void)handleDataone
{
    
    NSString *allStr = @"http://api.liwushuo.com/v1/channel_groups/all";
    [NetHandler getDataWithUrl:allStr completion:^(NSData *data) {
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
        NSDictionary *dic = [NSDictionary dictionary];
        //根据key： data取出数据中的字典
        dic = [result objectForKey:@"data"];
        NSMutableArray *allArr = [NSMutableArray array];
        //根据channel_groups取出数据中的字典
        allArr = [dic objectForKey:@"channel_groups"];
        //遍历取出来的数组得到字典数据
        for (NSDictionary *dicTemp in allArr) {
            NSMutableArray *channelsArr = [[NSMutableArray alloc] init];
            channelsArr = [dicTemp objectForKey:@"channels"];
            //创建一个空数组存放用字典初始化的对象
            NSMutableArray *arr = [NSMutableArray array];
            for (NSDictionary *dicTmp in channelsArr) {
                
                AllGroup *group = [[AllGroup alloc] initWithDictionary:dicTmp];
                // NSLog(@"%@",group.name);
                [arr addObject:group];
            }
            //以取出来的的name作为key并且刚得到的数组放入到属性字典中
                       NSString *str = [dicTemp objectForKey:@"name"];
            [self.categorydic setObject:arr forKey:str];
        }
        [self objectScrollView];

    }];
    

    
    
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.alpha = 1;
    self.navigationController.navigationBar.hidden = NO;
    
}

- (void)specialScrollView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.allScrollView = [[UIScrollView alloc]initWithFrame :CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
    [self.view addSubview:self.allScrollView];
    self.allScrollView.contentSize = CGSizeMake(0, self.view.bounds.size.height + 30);
//    self.allScrollView.bounces = NO;
    self.allScrollView.showsVerticalScrollIndicator = NO;
       //专题的tableView
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 10, 50, 20)];
    label.backgroundColor = [UIColor whiteColor];
    [self.allScrollView addSubview:label];
    label.text = @"专题";
    [label release];
    
    //查看全部按钮
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeSystem)];
    button.frame = CGRectMake(self.view.bounds.size.width - 80, 10, 70, 20);
    [self.allScrollView addSubview:button];
    button.backgroundColor = [UIColor whiteColor];
    [button addTarget:self action:@selector(button:) forControlEvents:(UIControlEventTouchUpInside)];
    [button setTitle:@"查看全部" forState:(UIControlStateNormal)];
    button.tintColor = [UIColor blackColor];
    
    
    
    //标题横向ScrollView
    self.view.backgroundColor = [UIColor whiteColor];
    self.specialScroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 35, self.view.bounds.size.width, (self.view.bounds.size.height / 4) - 35)];
    [self.allScrollView addSubview:self.specialScroll];
    self.specialScroll.showsVerticalScrollIndicator = NO;
    
    //滚动范围
    self.specialScroll.contentSize = CGSizeMake(self.view.bounds.size.width * 3, 0);
   
    self.specialScroll.showsHorizontalScrollIndicator = NO;
    
    for (int i = 0; i < 6; i++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:(CGRectMake(i * self.view.bounds.size.width / 2.0 + 10, 0, self.view.bounds.size.width / 2.0 - 10 , self.view.bounds.size.height / 4 - 40) )];
        [self.specialScroll addSubview:imageView];
        imageView.userInteractionEnabled = YES;
        //将手势放到objectimageView上
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionSpecial:)];
        
        [imageView addGestureRecognizer:tap];
        tap.numberOfTapsRequired = 1;
        [tap release];
        Special *special = [[Special alloc] init];
        special = [self.speciallArr objectAtIndex:i];
        [imageView setImageWithURL:[NSURL URLWithString:special.banner_image_url]];
        [imageView release];
    }
     [_specialScroll release];
}

#pragma mark objectScrollView
//对象的scrollView
- (void)objectScrollView
{
    

    UILabel *objectTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 ,self.view.bounds.size.height / 4 + 10 , 60, 20)];
    objectTitleLabel.text = @"对象";
    [self.allScrollView addSubview:objectTitleLabel];
    [objectTitleLabel release];
         //对象的scrollView
    
    
    //对象的scrollView
   self.obScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height / 4 + 35, self.view.bounds.size.width, self.view.bounds.size.height / 4 - 35)];
    [self.allScrollView addSubview:self.obScrollView];
    NSMutableArray *objectArr = [NSMutableArray arrayWithArray:[self.categorydic objectForKey:@"对象"]];
    //偏移量
    
    self.obScrollView.contentSize = CGSizeMake(self.view.bounds.size.width / 4 * 8, 0);
    //隐藏滚动条
    self.obScrollView.showsHorizontalScrollIndicator = NO;
    for (int i = 0; i < objectArr.count; i++) {
       //将imageVIew放到对象scrollVIew上
        UIImageView *objectImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 + i * self.view.bounds.size.width / 4, 0, self.view.bounds.size.width / 4 - Weight, self.view.bounds.size.width / 4 - Weight)];
        [self.obScrollView addSubview:objectImageView];
        objectImageView.userInteractionEnabled = YES;
        //将手势放到objectimageView上
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionObject:)];
        [objectImageView addGestureRecognizer:tap];
        tap.numberOfTapsRequired = 1;
        [tap release];
         [objectImageView release];
        
        //objectImageVIew上添加图片
        AllGroup *group = [[AllGroup alloc] init];
        group = [objectArr objectAtIndex:i];
        NSString *str = group.icon_url;
//         NSLog(@"-------%@", str);
        str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:str];
        [objectImageView setImageWithURL:url];

        
        
        
        //添加每个图片的标题
        UILabel *objectLabel = [[UILabel alloc] initWithFrame:CGRectMake(5 + i * self.view.bounds.size.width / 4, self.view.bounds.size.width / 4 - Weight  , self.view.bounds.size.width / 6, 20)];
        [self.obScrollView addSubview:objectLabel];
        objectLabel.text = group.name;
        objectLabel.font = [UIFont boldSystemFontOfSize:13];
        objectLabel.textAlignment = NSTextAlignmentCenter;
        
    }
    [_obScrollView release];
    
    //根据得到的数据处理字典，依据key取出数组显示下一页的collection
    NSMutableArray *personalityArr = [[NSMutableArray alloc] initWithArray:[self.categorydic objectForKey:@"个性"]];
    //设置personiltyScrollView 标题
    UILabel *pesoniltyTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.bounds.size.height / 2 + 10, 60, 20)];

    pesoniltyTitleLabel.text = @"个性";
    [self.allScrollView addSubview:pesoniltyTitleLabel];
    
    [pesoniltyTitleLabel release];
    //创建个性scrollView  添加到大得scrollVIew上
    self.personiltyScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height / 2 + 35 , self.view.bounds.size.width, 100)];
    //设置偏移量
    self.personiltyScrollView.contentSize = CGSizeMake(personalityArr.count * self.view.bounds.size.width / 4.0, 0) ;
    //隐藏scrollView的滚动条
    self.personiltyScrollView.showsHorizontalScrollIndicator = NO;
    [self.allScrollView  addSubview: self.personiltyScrollView];
    //循环在个性scrollView上加上imageView
    for (int i = 0; i < personalityArr.count; i++) {
        //创建接口类对象
        AllGroup *group = [[AllGroup alloc] init];
        //根据下标循环取出上边得到的数组的对象
        group = [personalityArr objectAtIndex:i];
        //创建并添加imageView到personiltyScrollView上
        UIImageView *personiltyImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 + i * self.view.bounds.size.width / 4, 0, self.view.bounds.size.width / 4 - Weight, self.view.bounds.size.width / 4 - Weight)];
        //personiltyImageView.backgroundColor = [UIColor redColor];
        [self.personiltyScrollView addSubview:personiltyImageView];
        //打开imageView的交互界面
        personiltyImageView.userInteractionEnabled = YES;
        //get方法得到图片的网址
        NSString *str = group.icon_url;
        str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:str];
        //解析根据第三方将图片显示到imageView上
        [personiltyImageView setImageWithURL:url];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionPersonilty:)];
        [personiltyImageView addGestureRecognizer:tap];
        tap.numberOfTapsRequired = 1;
        [tap release];
        [personiltyImageView release];
        
        
        
        
        //给每个imageView添加一个Label
        UILabel *personiltyLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 + i * self.view.bounds.size.width / 4, self.view.bounds.size.width / 4 - Weight + 10, self.view.bounds.size.width / 8, 20)];
        //get方法取出对象中的标题字符
        personiltyLabel.text = group.name;
        //设置字体
        personiltyLabel.font = [UIFont boldSystemFontOfSize:13];
        //Label中字体居中
        personiltyLabel.textAlignment = NSTextAlignmentCenter;
        [self.personiltyScrollView addSubview:personiltyLabel];
        [personiltyLabel release];
    }
    [_personiltyScrollView release];
  
    //定义一个空数组，来存放AllGroup对象
    NSMutableArray *accossioArr = [NSMutableArray array];
    //定义一个场合Label,并且添加到大scrollView上
    UILabel *accossionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, self.view.bounds.size.height * 3 / 4 + 10, 80, 20)];
    accossionLabel.text = @"场合";
    [self.allScrollView addSubview:accossionLabel];
    [accossionLabel release];
    accossioArr = [self.categorydic objectForKeyedSubscript:@"场合"];
    //场合scrollView
    self.occassionScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height * 3 / 4 + 35, self.view.bounds.size.width, 100)];
    //设置场合scrollView的偏移量
    self.occassionScrollView.contentSize = CGSizeMake(accossioArr.count * self.view.bounds.size.width / 4.0, 0) ;
    //影藏场合scrollView的偏移量
    self.occassionScrollView.showsHorizontalScrollIndicator = NO;
    [self.allScrollView  addSubview: self.occassionScrollView];
    //循环添加imageView在场合scrollVIew上
    for (int i = 0; i < accossioArr.count; i++) {
        //创建一个ALLgroup 对象
        AllGroup *group = [[AllGroup alloc] init];
        //根据下标的到对象
        group = [accossioArr objectAtIndex:i];
        UIImageView *accossioImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 + i * self.view.bounds.size.width / 4, 0, self.view.bounds.size.width / 4 - Weight, self.view.bounds.size.width / 4 - Weight)];
        //personiltyImageView.backgroundColor = [UIColor redColor];
        [self.occassionScrollView addSubview:accossioImageView];
        accossioImageView.userInteractionEnabled = YES;
        //得到场合scrollView上要添加图片的网址
        NSString *str = group.icon_url;
        str = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSURL *url = [NSURL URLWithString:str];
        //第三方显示图片
        [accossioImageView setImageWithURL:url];
            //添加手势到imageView上
        
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActionOccassion:)];
        [accossioImageView  addGestureRecognizer:tap];
        tap.numberOfTapsRequired = 1;
        [tap release];
        [accossioImageView release];

        
        //添加场合scrollVIew的每一个图片的标题
        UILabel *occasionViewLabel = [[UILabel alloc] initWithFrame:CGRectMake(15 + i * self.view.bounds.size.width / 4, self.view.bounds.size.width / 4 - Weight + 10, self.view.bounds.size.width / 8, 20)];
        occasionViewLabel.text = group.name;
        occasionViewLabel.textAlignment = NSTextAlignmentCenter;
        occasionViewLabel.font = [UIFont boldSystemFontOfSize:13];
        [self.occassionScrollView addSubview:occasionViewLabel];
        [occasionViewLabel release];
    }
    [_occassionScrollView release];
    [_allScrollView release];
}



- (void)viewDidLoad {
    
    self.navigationItem.title = @"分类";
    self.navigationController.navigationBar.barTintColor = [UIColor ownColor];
    self.navigationController.navigationBar.translucent = NO;
//    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigation_bar_background.png"] forBarMetrics:UIBarMetricsDefault];
//    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor,nil]];
    

    [self handleData];
    
    // 抽屉按钮
    [self setupLeftMenuButton];
       // Do any additional setup after loading the view.

}

#pragma mark 抽屉按钮
// 设置navigation的左按钮为抽屉开关
- (void)setupLeftMenuButton
{
    MMDrawerBarButtonItem * leftDrawerButton = [[MMDrawerBarButtonItem alloc] initWithTarget:self action:@selector(leftDrawerButtonPress:)];
    [self.navigationItem setLeftBarButtonItem:leftDrawerButton animated:YES];
}
// 触发的方法
- (void)leftDrawerButtonPress:(id)sender{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}


- (void)tapActionSpecial:(UITapGestureRecognizer *)tap
{
    CGPoint point = [tap locationInView:self.specialScroll];
    int index = 2 * point.x / self.view.bounds.size.width;

    Special *special  = [self.speciallArr objectAtIndex:index];
   AllButtonController *objectVC = [[AllButtonController alloc] init];
    objectVC.special = special;
    [self.navigationController pushViewController:objectVC animated:YES];
    [objectVC release];

}

- (void)tapActionObject:(UITapGestureRecognizer *)tap
{
    
    CGPoint point = [tap locationInView:self.obScrollView];
    int index = 4 * point.x / self.view.bounds.size.width;
    NSMutableArray *personiltyArr = [NSMutableArray arrayWithArray:[self.categorydic objectForKey:@"对象"]];
    AllGroup *group  = [personiltyArr objectAtIndex:index];
    //NSLog(@"%@", group.title);
    PersoniltyViewController *personiltyVC = [[PersoniltyViewController alloc] init];
    personiltyVC.group = group;
    [self.navigationController pushViewController:personiltyVC animated:YES];
    [personiltyVC release];
}

- (void)tapActionPersonilty:(UITapGestureRecognizer *)tap
{
    CGPoint point = [tap locationInView:self.personiltyScrollView];
    int index = 4 * point.x / self.view.bounds.size.width;
    NSLog(@"%d", index);
    NSMutableArray *personiltyArr = [NSMutableArray arrayWithArray:[self.categorydic objectForKey:@"个性"]];
    AllGroup *group  = [personiltyArr objectAtIndex:index];
    //NSLog(@"%@", group.title);
    PersoniltyViewController *personiltyVC = [[PersoniltyViewController alloc] init];
    personiltyVC.group = group;
    [self.navigationController pushViewController:personiltyVC animated:YES];
    [personiltyVC release];
}
- (void)tapActionOccassion:(UITapGestureRecognizer *)tap
{
    CGPoint point = [tap locationInView:self.occassionScrollView];
    int index = 4 * point.x / self.view.bounds.size.width;
    NSLog(@"%d", index);
    NSMutableArray *personiltyArr = [NSMutableArray arrayWithArray:[self.categorydic objectForKey:@"场合"]];
    AllGroup *group  = [personiltyArr objectAtIndex:index];
    //NSLog(@"%@", group.title);
    PersoniltyViewController *personiltyVC = [[PersoniltyViewController alloc] init];
    personiltyVC.group = group;
    
    [self.navigationController pushViewController:personiltyVC animated:YES];
    [personiltyVC release];
}
- (void)button:(UIButton *)all
{
   
    
    SpecialDetailViewController *controller = [[SpecialDetailViewController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
    
}

//专题的第一个按钮实现
- (void)special1:(UIButton *)hehe
{
    NSLog(@"我是专题1");
}

//专题的第二个按钮实现
- (void)special2:(UIButton *)hehe
{
    NSLog(@"我是专题2");
}

//专题的第三个按钮实现
- (void)special3:(UIButton *)hehe
{
    NSLog(@"我是专题3");
}

//专题的第四个按钮实现
- (void)special4:(UIButton *)hehe
{
    NSLog(@"我是专题4");
}

//专题的第五个按钮实现
- (void)special5:(UIButton *)hehe
{
    NSLog(@"我是专题5");
}


//专题的第六个按钮实现
- (void)special6:(UIButton *)hehe
{
    NSLog(@"我是专题6");
}




//对象的第一个按钮实现
- (void)dx1:(UIButton *)hehe
{
    NSLog(@"我是对象1");
}

//对象的第二个按钮实现
- (void)dx2:(UIButton *)hehe
{
    NSLog(@"我是对象2");
}

//对象的第三个按钮实现
- (void)dx3:(UIButton *)hehe
{
    NSLog(@"我是对象3");
}

//对象的第一四个按钮实现
- (void)dx4:(UIButton *)hehe
{
    NSLog(@"我是对象4");
}

//对象的第五个按钮实现
- (void)dx5:(UIButton *)hehe
{
    NSLog(@"我是对象5");
}
//对象的第六个按钮实现
- (void)dx6:(UIButton *)hehe
{
    NSLog(@"我是对象6");
}
//对象的第七个按钮实现
- (void)dx7:(UIButton *)hehe
{
    NSLog(@"我是对象7");
}
//对象的第八个按钮实现
- (void)dx8:(UIButton *)hehe
{
    NSLog(@"我是对象8");
}



//礼物的第一个按钮实现
- (void)lw1:(UIButton *)hehe
{
    NSLog(@"我是礼物1");
}

//礼物的第二个按钮实现
- (void)lw2:(UIButton *)hehe
{
    NSLog(@"我是礼物2");
}

//礼物的第三个按钮实现
- (void)lw3:(UIButton *)hehe
{
    NSLog(@"我是礼物3");
}

//礼物的第四个按钮实现
- (void)lw4:(UIButton *)hehe
{
    NSLog(@"我是礼物4");
}

//礼物的第五个按钮实现
- (void)lw5:(UIButton *)hehe
{
    NSLog(@"我是礼物5");
}
//礼物的第6个按钮实现
- (void)lw6:(UIButton *)hehe
{
    NSLog(@"我是礼物6");
}
//礼物的第7个按钮实现
- (void)lw7:(UIButton *)hehe
{
    NSLog(@"我是礼物7");
}






//场合的第一个按钮实现
- (void)ch1:(UIButton *)hehe
{
    NSLog(@"我是场合1");
}

//场合的第二个按钮实现
- (void)ch2:(UIButton *)hehe
{
    NSLog(@"我是场合2");
}

//场合的第三个按钮实现
- (void)ch3:(UIButton *)hehe
{
    NSLog(@"我是场合3");
}

//场合的第四个按钮实现
- (void)ch4:(UIButton *)hehe
{
    NSLog(@"我是场合4");
}

//场合的第五个按钮实现
- (void)ch5:(UIButton *)hehe
{
    NSLog(@"我是场合5");
}








- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
